package battleship;

import java.util.Random;
import java.util.Scanner;

public class Board {
	
	private int[][] boardP1 = new int[10][10];
    private int[][] shipsP1 = new int[20][2];
    private int[] shootP1 = new int[2];
    private int[][] boardP2 = new int[10][10];
    private int[][] shipsP2 = new int[20][2];
    private int[] shootP2 = new int[2];
    public boolean player1Turn = true;

public  void initBoardP1(){
    for(int row=0 ; row < 10 ; row++ )
        for(int column=0 ; column < 10 ; column++ )
            boardP1[row][column]=-1;
}

public  void showBoardP1(){
    System.out.println("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9 \t10 ");
    System.out.println();
    
    for(int row=0 ; row < 10 ; row++ ){
        System.out.print((row+1)+"");
        for(int column=0 ; column < 10 ; column++ ){
            if(boardP1[row][column]==-1){
                System.out.print("\t"+"+");
            }else if(boardP1[row][column]==0){
                System.out.print("\t"+"O");
            }else if(boardP1[row][column]==1){
                System.out.print("\t"+"X");
            }
            
        }
        System.out.println();
    }

}

public void initShipsP1(){
    Random random = new Random();
    
    for(int ship=0 ; ship < 20 ; ship++){
        shipsP1[ship][0]=random.nextInt(10);
        shipsP1[ship][1]=random.nextInt(10);
        
        //let's check if that shot was already tried 
        //if it was, just finish the do...while when a new pair was randomly selected
        for(int last=0 ; last < ship ; last++){
            if( (shipsP1[ship][0] == shipsP1[last][0])&&(shipsP1[ship][1] == shipsP1[last][1]) ){
                do{
                    shipsP1[ship][0]=random.nextInt(10);
                    shipsP1[ship][1]=random.nextInt(10);
                }while( (shipsP1[ship][0] == shipsP1[last][0])&&(shipsP1[ship][1] == shipsP1[last][1]) );
        }
        }
    }
}

public void shootP1(){
    Scanner input = new Scanner(System.in);
    
    System.out.print("Row: ");
    shootP1[0] = input.nextInt();
    shootP1[0]--;
    
    System.out.print("Column: ");
    shootP1[1] = input.nextInt();
    shootP1[1]--;
    
}

public boolean hitP1(){
    
    for(int ship=0 ; ship<shipsP1.length ; ship++){
        if( shootP1[0]==shipsP1[ship][0] && shootP1[1]==shipsP1[ship][1]){
            System.out.printf("You hit a ship located in (%d,%d)\n",shootP1[0]+1,shootP1[1]+1);
            return true;
        }
    }
    return false;
}


public void changeboardP1(){
    if(hitP1())
        boardP1[shootP1[0]][shootP1[1]]=1;
    else
        boardP1[shootP1[0]][shootP1[1]]=0;
}



public  void initBoardP2(){
for(int row=0 ; row < 10 ; row++ )
    for(int column=0 ; column < 10 ; column++ )
        boardP2[row][column]=-1;
}

public  void showBoardP2(){
System.out.println("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9 \t10 ");
System.out.println();

for(int row=0 ; row < 10 ; row++ ){
    System.out.print((row+1)+"");
    for(int column=0 ; column < 10 ; column++ ){
        if(boardP2[row][column]==-1){
            System.out.print("\t"+"+");
        }else if(boardP2[row][column]==0){
            System.out.print("\t"+"O");
        }else if(boardP2[row][column]==1){
            System.out.print("\t"+"X");
        }
        
    }
    System.out.println();
}

}

public void initShipsP2(){
Random random = new Random();

for(int ship=0 ; ship < 20 ; ship++){
    shipsP2[ship][0]=random.nextInt(10);
    shipsP2[ship][1]=random.nextInt(10);
    
    //let's check if that shot was already tried 
    //if it was, just finish the do...while when a new pair was randomly selected
    for(int last=0 ; last < ship ; last++){
        if( (shipsP2[ship][0] == shipsP2[last][0])&&(shipsP2[ship][1] == shipsP2[last][1]) ){
            do{
                shipsP2[ship][0]=random.nextInt(10);
                shipsP2[ship][1]=random.nextInt(10);
            }while( (shipsP2[ship][0] == shipsP2[last][0])&&(shipsP2[ship][1] == shipsP2[last][1]) );
    }
    }
}
}

public void shootP2(){
Scanner input = new Scanner(System.in);

System.out.print("Row: ");
shootP2[0] = input.nextInt();
shootP2[0]--;

System.out.print("Column: ");
shootP2[1] = input.nextInt();
shootP2[1]--;

}

public boolean hitP2(){

for(int ship=0 ; ship<shipsP2.length ; ship++){
    if( shootP2[0]==shipsP2[ship][0] && shootP2[1]==shipsP2[ship][1]){
        System.out.printf("You hit a ship located in (%d,%d)\n",shootP2[0]+1,shootP2[1]+1);
        return true;
    }
}
return false;
}


public void changeboardP2(){
if(hitP2())
    boardP2[shootP2[0]][shootP2[1]]=1;
else
    boardP2[shootP2[0]][shootP2[1]]=0;
}

}


